package com.marvels.dubbo.externalservice.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.fintech.third.constant.ResponseResult;
import com.fintech.third.constant.SmsTypeEnum;
import com.fintech.third.model.sms.SmsParameter;
import com.fintech.third.model.sms.SmsTemplateNew;
import com.fintech.third.service.sms.SmsService;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.dubbo.externalservice.dto.SendMsgDTO;
import com.marvels.dubbo.externalservice.service.SendSmsNewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SendSmsNewServiceImpl implements SendSmsNewService {

	@Autowired
	private SmsService smsService;
	
	@Override
	public SmsTemplateNew getSmsTemplateNew(SendMsgDTO sendMsgDTO) {
		MarvelsLogUtil.getInstance().info("获取短信模板信息参数：" + JSONObject.toJSONString(sendMsgDTO));
		SmsTemplateNew smsTemplateNew = smsService.getSmsTemplateNew(getSmsTypeEnum(sendMsgDTO.getTemplateName()));
		MarvelsLogUtil.getInstance().info("获取短信模板信息结果：" + JSONObject.toJSONString(smsTemplateNew));
		return smsTemplateNew;
	}

	@Override
	public boolean sendMsg(SendMsgDTO sendMsgDTO) {
		boolean result = false;
		String dtoStr = JSONObject.toJSONString(sendMsgDTO);
		MarvelsLogUtil.getInstance().info("发送短信参数：" + dtoStr);
		SmsParameter smsParameter = JSONObject.parseObject(dtoStr, SmsParameter.class);
		ResponseResult responseResult = smsService.sendSms(sendMsgDTO.getMobielNo(), smsParameter, getSmsTypeEnum(sendMsgDTO.getTemplateName()));
		MarvelsLogUtil.getInstance().info("发送短信结果：" + JSONObject.toJSONString(responseResult));
		if(responseResult.getErrorCode().equals("100000")){
			result = true;
		}
		return result;
	}

	private SmsTypeEnum getSmsTypeEnum(String templateName){
		SmsTypeEnum smsTypeEnum = null;
		for (SmsTypeEnum e : SmsTypeEnum.values()) {
			if(e.getTemplateName().equals(templateName)){
				smsTypeEnum = e;
				break;
			}
		}
		return smsTypeEnum;
	}
}
